package com.zhuxulin.tab;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.zhuxulin.midi.util.MidiProcessor;
import com.zhuxulin.tab.bluetooth.PlayMusic;
import com.zhuxulin.tab.datas.CloseChannelEventData;
import com.zhuxulin.tab.threads.PlayMidOnPiano;
import com.zhuxulin.tab.threads.PlayPiano;
import com.zhuxulin.tab.utils.DataUtils;
import com.zhuxulin.tab.utils.LogUtils;

public class Piano extends Activity {
	protected static final String TAG = Piano.class.getSimpleName();
	private Button start = null;
	private Button stop = null;
	private Button back = null;
	private App app = null;
	private ListView listMusic = null;
	private SimpleAdapter adapter = null;
//	private PlayPiano onPiano = null;
	private File root = new File(Environment.getExternalStorageDirectory()
			+ "/Music");
	private File currentDir = null;
	private PlayMusic playMusic = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_piano);
		app = (App) this.getApplication();
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.Stop);
		back = (Button) findViewById(R.id.back);
		listMusic = (ListView) findViewById(R.id.list_music);
//		onPiano = new PlayPiano(app);
		playMusic = app.getPlayMusic();
		initListView(root);
	}

	private void initListView(File root) {
		adapter = new SimpleAdapter(this, DataUtils.getListMap(root),
				R.layout.list_item, new String[] { "title", "info", "img",
						"file", "size" }, new int[] { R.id.title, R.id.info,
						R.id.img, R.id.file, R.id.size });
		listMusic.setAdapter(adapter);
	}

	@Override
	protected void onStart() {
		super.onStart();
		listMusic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LogUtils.i(TAG, "pos:" + position + "\tid:" + id);
				@SuppressWarnings("unchecked")
				Map<String, Object> map = (HashMap<String, Object>) listMusic
						.getItemAtPosition(position);
				String title = (String) map.get("title");
				File f = (File) map.get("file");
				LogUtils.i(TAG, title);
				LogUtils.i(TAG, f.getName());
				if (f.isDirectory()) {
					back.setEnabled(true);
					currentDir = f;
					initListView(f);
					// adapter = new SimpleAdapter(Piano.this, DataUtils
					// .getListMap(f), R.layout.list_item, new String[] {
					// "title", "info", "img", "file","size" }, new int[] {
					// R.id.title, R.id.info, R.id.img, R.id.file,R.id.size });
					// listMusic.setAdapter(adapter);
				} else {
					LogUtils.i(TAG, "BeginPlayOnPiano!");
					if (app.getSendMsg() != null) {
						app.getSendMsg().sendMessage(
								CloseChannelEventData.closeALL());
						playMusic.setPlayFile(f);
						playMusic.play();
						// new PlayPiano(app,f).start();
						// onPiano.isAlive();
						// onPiano.setFile(f);
						// onPiano.run();
						// onPiano.start();
						// if(onPiano==null){
						// onPiano=new PlayPiano(app);
						// }else if(onPiano.isStart()){
						// onPiano.Stop();
						// onPiano=new PlayPiano(app);
						// // onPiano.setFile(f);
						// // onPiano.start();
						// }
						new PlayPiano(app).setFile(f).start();
//						onPiano.setFile(f);
//						onPiano.start();

					}
				}
			}
		});
		listMusic
				.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						LogUtils.i(TAG, "pos:" + position + "\tid:" + id);
						return false;
					}
				});
		start.setOnClickListener(new View.OnClickListener() {
			// Thread onPiano=null;
			@Override
			public void onClick(View v) {
				LogUtils.i(TAG, "Send msg!");
				// // Note on
				// sendMsg.sendMessage(0x90);
				// // Note 3C
				// sendMsg.sendMessage(0x3C);
				// // Note off
				// sendMsg.sendMessage(0x7F);
				// File file = new File("/mnt/sdcard/Music/ch38_1.mid");
				// onPiano=new PlayMidOnPiano(app, file);
				// if(onPiano.isAlive()){
				// onPiano.stop();
				// }else{
//				onPiano.start();
				// }
				// playPaino.stop(null);
			}
		});
		stop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MidiProcessor processor = app.getProcessor();
				if (processor == null) {
					return;
				}
				processor.pause();
			}
		});
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!currentDir.getPath().equals(root.getPath())) {
					currentDir = currentDir.getParentFile();
					initListView(currentDir);
				}
				if (currentDir.getPath().equals(root.getPath())) {
					back.setEnabled(false);
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.piano, menu);
		return true;
	}
}
